package com.shangqing.system.service.impl;

import com.shangqing.system.domain.caoyuan.vo.CityVo;
import com.shangqing.system.domain.coldwave.Coldwave;
import com.shangqing.system.mapper.CityMapper;
import com.shangqing.system.mapper.ColdwaveEarlyMapper;
import com.shangqing.system.mapper.ColdwaveGradeMapper;
import com.shangqing.system.mapper.ColdwaveMapper;
import com.shangqing.system.service.ColdwaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ColdwaveServiceImpl implements ColdwaveService {
    @Autowired
    private ColdwaveMapper coldwaveMapper;

    @Autowired
    private ColdwaveEarlyMapper coldwaveEarlyMapper;

    @Autowired
    private ColdwaveGradeMapper coldwaveGradeMapper;
    @Autowired
    private CityMapper cityMapper;

    @Override
    public List<Coldwave> selectall() {
        List<Coldwave> coldwaves = new ArrayList<>();
        List<Coldwave> coldwaveList = coldwaveMapper.selectall();
        for (Coldwave codew : coldwaveList
        ) {
            codew.setColdwaveEarly(coldwaveEarlyMapper.selectById(codew.getEarlyId()));
            codew.setColdwaveGrade(coldwaveGradeMapper.selectById(codew.getGradeId()));
            CityVo city = cityMapper.select(codew.getCityId());//获取当前地址
            city.setCities(cityVos(city));// 递归获取地址
            codew.setCityVo(city);
            coldwaves.add(codew);
        }

        return coldwaves;
    }

    private CityVo cityVos(CityVo list) {
        if (list.getParentId() == 0) {
            return null;      //递归出口
        } else {
            CityVo vo = cityMapper.select(list.getParentId());//获取父ID对应的地址
            CityVo cityVo = new CityVo();//封装
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCities(cityVos(vo)); //进行递归进口
            return cityVo;
        }
    }


}
